package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;

import com.moleq.models.BottleSetting;
import com.moleq.models.Departmant;
import com.moleq.models.PosLineCoupon;
import com.moleq.models.PosLineDiscnt;
import com.moleq.models.PosLineItem;
import com.moleq.models.PosLineTares;
import com.moleq.models.TaxSetting;

public class PosLineCouponService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;

	public PosLineCouponService() {

	}

	public PosLineCouponService(Context context) {
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public List<PosLineCoupon> FindAll(){
		List<PosLineCoupon> obj = new ArrayList<PosLineCoupon>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.PosLineCoupon.SCHEMA_NAME,null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		
		while(cursor.moveToNext()){
			int lc_id  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineCoupon.LC_ID));
			int lc_line_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineCoupon.LC_LINE_NO));
			String lc_type  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCoupon.LC_TYPE));
			double lc_value = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PosLineCoupon.LC_VALUE));
			
			obj.add(new PosLineCoupon(lc_id, lc_line_no, lc_type, lc_value));
		}
		cursor.close();
		return obj;
	}
	
	public  List<PosLineCoupon> Find(long no){
		
		List<PosLineCoupon> obj = new ArrayList<PosLineCoupon>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.PosLineCoupon.SCHEMA_NAME +" where lc_line_no = "+no,null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int lc_id  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineCoupon.LC_ID));
			int lc_line_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineCoupon.LC_LINE_NO));
			String lc_type  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCoupon.LC_TYPE));
			double lc_value = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PosLineCoupon.LC_VALUE));
			
			obj.add(new  PosLineCoupon(lc_id, lc_line_no, lc_type, lc_value));
		}
		cursor.close();
		return obj;
	
	}
	
	public void Insert(PosLineCoupon obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO " + DatabaseSchema.PosLineCoupon.SCHEMA_NAME + " VALUES(?,?,?,?)",
				new Object[] { null, obj.getLc_line_no(), obj.getLc_type(), obj.getLc_value() });
	}
	
	public void DeleteByNo(long no) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosLineCoupon.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.PosLineCoupon.LC_LINE_NO  +" = ?",
				new Object[] { no });
	}
	
	public void DeleteByCouponNo(long no) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosLineCoupon.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.PosLineCoupon.LC_ID  +" = ?",
				new Object[] { no });
	}
	
	public void DeleteByAll() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosLineCoupon.SCHEMA_NAME);
	} 
	
	public Integer getMaxNo(){
		int no = 0;
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select max(lc_id) from " + DatabaseSchema.PosLineCoupon.SCHEMA_NAME, null);
		if (cursor.moveToNext()) {
			no = cursor.getInt(0);
		}
		cursor.close();
		return no;
	}
	
	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};
}
